package com.micangcaihang.micang.mvp.task.presenter;

import com.micangcaihang.common.baserx.RxSchedulers;
import com.micangcaihang.common.http.ApiResult;
import com.micangcaihang.common.http.RxHelper;
import com.micangcaihang.common.http.exception.ApiException;
import com.micangcaihang.common.utils.ToastUtils;
import com.micangcaihang.micang.entity.PickerEntity;
import com.micangcaihang.micang.entity.TuftInfoEntity;
import com.micangcaihang.micang.http.MiSubScriber;
import com.micangcaihang.micang.http.request.TaskTuftRequest;
import com.micangcaihang.micang.mvp.task.contract.TaskTuftInfoContract;
import com.micangcaihang.micang.mvp.task.model.TaskTuftInfoModel;

import java.util.List;

import io.reactivex.Observable;
import io.reactivex.ObservableSource;
import io.reactivex.annotations.NonNull;
import io.reactivex.functions.Function;

/**
 * <pre>
 *     author : Fandy
 *     e-mail : fandayongfly@gmail.com
 *     time   : 2017/07/17
 *     desc   :
 *     version: 1.0
 * </pre>
 */

public class TaskTuftInfoPresenter extends TaskTuftInfoContract.Presenter {
    private TaskTuftInfoModel mModel;
    private String id;

    @Override
    public void setV(TaskTuftInfoContract.View v) {
        super.setV(v);
        mModel = new TaskTuftInfoModel();
    }

    public void setId(String id) {
        this.id = id;
    }

    /**
     * 每次进入界面获取网络数据，更新界面
     *
     * @param isRefresh 新增参数，用来判断是否为下拉刷新调用，下拉刷新的时候不应该再显示加载界面和异常界面
     */
    @Override
    public void loadFirstData(boolean isRefresh) {
        Observable<ApiResult<TuftInfoEntity>> tuftInfoData = mModel.getTuftInfoData(id);
        mRxManage.add(tuftInfoData.subscribeWith(new MiSubScriber<ApiResult<TuftInfoEntity>>() {

            @Override
            protected void doOnStart() {
                mView.showLoading();
            }

            @Override
            protected void doOnNext(ApiResult<TuftInfoEntity> result) {
                if (result.data != null) {
                    mView.returnTuftInfoData(result.data);
                }
            }

            @Override
            protected void doOnComplete() {
                mView.showContent();
            }

            @Override
            protected void doOnError(ApiException e) {
//                super.doOnError(e);
                mView.showNetError(e);
            }
        }));
    }

    /**
     * 加载更多数据
     */
    @Override
    public void loadMoreData() {

    }

    /**
     * 环路区间方向
     */
    @Override
    public void loadRingRdDirectionData() {
        Observable<ApiResult<List<PickerEntity>>> observable = mModel.getRingRdDirection();
        mRxManage.add(observable
                .doOnNext(listApiResult -> mView.returnRingRdDirectionData(listApiResult.data))
                .compose(RxSchedulers.observableIoToMain())
                .concatMap(new Function<ApiResult<List<PickerEntity>>, ObservableSource<ApiResult<List<PickerEntity>>>>() {

                    @Override
                    public ObservableSource<ApiResult<List<PickerEntity>>> apply(@NonNull ApiResult<List<PickerEntity>> listApiResult) throws Exception {
                        return mModel.getCirclePosition();
                    }
                })
                .subscribeWith(new MiSubScriber<ApiResult<List<PickerEntity>>>() {
                    @Override
                    protected void doOnStart() {
                        mView.showDialogLoading("");
                    }

                    @Override
                    protected void doOnNext(ApiResult<List<PickerEntity>> listApiResult) {
                        if (listApiResult.data != null && !listApiResult.data.isEmpty()) {
                            mView.returnCirclePositionData(listApiResult.data);
                        } else {
                            mView.showTip("暂时没有环路区间方向信息");
                        }
                    }

                    @Override
                    protected void doOnError(ApiException e) {
                        super.doOnError(e);
                        mView.hideDialogLoading();
                    }

                    @Override
                    protected void doOnComplete() {
                        mView.hideDialogLoading();
                    }
                }));
    }

    /**
     * 房屋结构
     */
    @Override
    public void loadRoomConstructionData() {
        Observable<ApiResult<List<PickerEntity>>> observable = mModel.getRoomConstruction();
        mRxManage.add(observable.subscribeWith(new MiSubScriber<ApiResult<List<PickerEntity>>>() {
            @Override
            protected void doOnStart() {
                mView.showDialogLoading("");
            }

            @Override
            protected void doOnNext(ApiResult<List<PickerEntity>> listApiResult) {
                if (listApiResult.data != null && !listApiResult.data.isEmpty()) {
                    mView.returnRoomConstructionData(listApiResult.data);
                } else {
                    mView.showTip("暂时没有房屋结构信息");
                }
            }

            @Override
            protected void doOnError(ApiException e) {
                super.doOnError(e);
                mView.hideDialogLoading();
            }

            @Override
            protected void doOnComplete() {
                mView.hideDialogLoading();
            }
        }));
    }

    /**
     * 环路区间环数
     */
    @Override
    public void loadCirclePositionData() {

    }

    /**
     * 房屋用途
     */
    @Override
    public void loadHouseUseData() {
        Observable<ApiResult<List<PickerEntity>>> observable = mModel.getHouseUse();
        mRxManage.add(observable.subscribeWith(new MiSubScriber<ApiResult<List<PickerEntity>>>() {
            @Override
            protected void doOnStart() {
                mView.showDialogLoading("");
            }

            @Override
            protected void doOnNext(ApiResult<List<PickerEntity>> listApiResult) {
                if (listApiResult.data != null && !listApiResult.data.isEmpty()) {
                    mView.returnHouseUseData(listApiResult.data);
                } else {
                    mView.showTip("暂时没有房屋用途信息");
                }
            }

            @Override
            protected void doOnError(ApiException e) {
                super.doOnError(e);
                mView.hideDialogLoading();
            }

            @Override
            protected void doOnComplete() {
                mView.hideDialogLoading();
            }
        }));
    }

    /**
     * 产权类型
     */
    @Override
    public void loadPropertyRightTypeData() {
        Observable<ApiResult<List<PickerEntity>>> observable = mModel.getPropertyRightType();
        mRxManage.add(observable.subscribeWith(new MiSubScriber<ApiResult<List<PickerEntity>>>() {
            @Override
            protected void doOnStart() {
                mView.showDialogLoading("");
            }

            @Override
            protected void doOnNext(ApiResult<List<PickerEntity>> listApiResult) {
                if (listApiResult.data != null && !listApiResult.data.isEmpty()) {
                    mView.returnPropertyRightTypeData(listApiResult.data);
                } else {
                    mView.showTip("暂时没有产权类型信息");
                }
            }

            @Override
            protected void doOnError(ApiException e) {
                super.doOnError(e);
                mView.hideDialogLoading();
            }

            @Override
            protected void doOnComplete() {
                mView.hideDialogLoading();
            }
        }));
    }

    /**
     * 房屋朝阳
     */
    @Override
    public void loadHourseOrientationData() {
        Observable<ApiResult<List<PickerEntity>>> observable = mModel.getHourseOrientation();
        mRxManage.add(observable.subscribeWith(new MiSubScriber<ApiResult<List<PickerEntity>>>() {
            @Override
            protected void doOnStart() {
                mView.showDialogLoading("");
            }

            @Override
            protected void doOnNext(ApiResult<List<PickerEntity>> listApiResult) {
                if (listApiResult.data != null && !listApiResult.data.isEmpty()) {
                    mView.returnHourseOrientationData(listApiResult.data);
                } else {
                    mView.showTip("暂时没有房屋朝向信息");
                }
            }

            @Override
            protected void doOnError(ApiException e) {
                super.doOnError(e);
                mView.hideDialogLoading();
            }

            @Override
            protected void doOnComplete() {
                mView.hideDialogLoading();
            }
        }));
    }

    /**
     * 装修状况
     */
    @Override
    public void loadFitmentStatusData() {
        Observable<ApiResult<List<PickerEntity>>> observable = mModel.getFitmentStatus();
        mRxManage.add(observable.subscribeWith(new MiSubScriber<ApiResult<List<PickerEntity>>>() {
            @Override
            protected void doOnStart() {
                mView.showDialogLoading("");
            }

            @Override
            protected void doOnNext(ApiResult<List<PickerEntity>> listApiResult) {
                if (listApiResult.data != null && !listApiResult.data.isEmpty()) {
                    mView.returnFitmentStatusData(listApiResult.data);
                } else {
                    mView.showTip("暂时没有装修状况信息");
                }
            }

            @Override
            protected void doOnError(ApiException e) {
                super.doOnError(e);
                mView.hideDialogLoading();
            }

            @Override
            protected void doOnComplete() {
                mView.hideDialogLoading();
            }
        }));
    }

    /**
     * 建筑类型
     */
    @Override
    public void loadStructureTypeData() {
        Observable<ApiResult<List<PickerEntity>>> observable = mModel.getStructureType();
        mRxManage.add(observable.subscribeWith(new MiSubScriber<ApiResult<List<PickerEntity>>>() {
            @Override
            protected void doOnStart() {
                mView.showDialogLoading("");
            }

            @Override
            protected void doOnNext(ApiResult<List<PickerEntity>> listApiResult) {
                if (listApiResult.data != null && !listApiResult.data.isEmpty()) {
                    mView.returnStructureTypeData(listApiResult.data);
                } else {
                    mView.showTip("暂时没有建筑类型信息");
                }
            }

            @Override
            protected void doOnError(ApiException e) {
                super.doOnError(e);
                mView.hideDialogLoading();
            }

            @Override
            protected void doOnComplete() {
                mView.hideDialogLoading();
            }
        }));
    }

    /**
     * 使用状态
     */
    @Override
    public void loadUseinStatueData() {
        Observable<ApiResult<List<PickerEntity>>> observable = mModel.getUseinStatue();
        mRxManage.add(observable.subscribeWith(new MiSubScriber<ApiResult<List<PickerEntity>>>() {
            @Override
            protected void doOnStart() {
                mView.showDialogLoading("");
            }

            @Override
            protected void doOnNext(ApiResult<List<PickerEntity>> listApiResult) {
                if (listApiResult.data != null && !listApiResult.data.isEmpty()) {
                    mView.returnUseinStatueData(listApiResult.data);
                } else {
                    mView.showTip("暂时没有使用状态信息");
                }
            }

            @Override
            protected void doOnError(ApiException e) {
                super.doOnError(e);
                mView.hideDialogLoading();
            }

            @Override
            protected void doOnComplete() {
                mView.hideDialogLoading();
            }
        }));
    }

    /**
     * 整体结构
     */
    @Override
    public void loadStructureModifyData() {
        Observable<ApiResult<List<PickerEntity>>> observable = mModel.getStructureModify();
        mRxManage.add(observable.subscribeWith(new MiSubScriber<ApiResult<List<PickerEntity>>>() {
            @Override
            protected void doOnStart() {
                mView.showDialogLoading("");
            }

            @Override
            protected void doOnNext(ApiResult<List<PickerEntity>> listApiResult) {
                if (listApiResult.data != null && !listApiResult.data.isEmpty()) {
                    mView.returnStructureModifyData(listApiResult.data);
                } else {
                    mView.showTip("暂时没有整体结构信息");
                }
            }

            @Override
            protected void doOnError(ApiException e) {
                super.doOnError(e);
                mView.hideDialogLoading();
            }

            @Override
            protected void doOnComplete() {
                mView.hideDialogLoading();
            }
        }));
    }

    /**
     * 唯一房产
     */
    @Override
    public void loadIsOnlyHouseData() {
        Observable<ApiResult<List<PickerEntity>>> observable = mModel.getIsOnlyHouse();
        mRxManage.add(observable.subscribeWith(new MiSubScriber<ApiResult<List<PickerEntity>>>() {
            @Override
            protected void doOnStart() {
                mView.showDialogLoading("");
            }

            @Override
            protected void doOnNext(ApiResult<List<PickerEntity>> listApiResult) {
                if (listApiResult.data != null && !listApiResult.data.isEmpty()) {
                    mView.returnIsOnlyHouseData(listApiResult.data);
                } else {
                    mView.showTip("暂时没有唯一房产信息");
                }
            }

            @Override
            protected void doOnError(ApiException e) {
                super.doOnError(e);
                mView.hideDialogLoading();
            }

            @Override
            protected void doOnComplete() {
                mView.hideDialogLoading();
            }
        }));
    }

    /**
     * 购买年数
     */
    @Override
    public void loadPurchaseYearData() {
        Observable<ApiResult<List<PickerEntity>>> observable = mModel.getPurchaseYear();
        mRxManage.add(observable.subscribeWith(new MiSubScriber<ApiResult<List<PickerEntity>>>() {
            @Override
            protected void doOnStart() {
                mView.showDialogLoading("");
            }

            @Override
            protected void doOnNext(ApiResult<List<PickerEntity>> listApiResult) {
                if (listApiResult.data != null && !listApiResult.data.isEmpty()) {
                    mView.returnPurchaseYearData(listApiResult.data);
                } else {
                    mView.showTip("暂时没有购买年数信息");
                }
            }

            @Override
            protected void doOnError(ApiException e) {
                super.doOnError(e);
                mView.hideDialogLoading();
            }

            @Override
            protected void doOnComplete() {
                mView.hideDialogLoading();
            }
        }));
    }

    @Override
    public void saveTuftInfo(TaskTuftRequest request) {
        Observable<ApiResult<Void>> observable = mModel.savaTuftInfo(request);
        Observable<ApiResult<TuftInfoEntity>> tuftInfoData = mModel.getTuftInfoData(id);
        mRxManage.add(observable.concatMap(new Function<ApiResult<Void>, ObservableSource<ApiResult<TuftInfoEntity>>>() {
            @Override
            public ObservableSource<ApiResult<TuftInfoEntity>> apply(@NonNull ApiResult<Void> voidApiResult) throws Exception {
                return tuftInfoData;
            }
        }).compose(RxSchedulers.observableIoToMain())
                .compose(RxHelper.handleResult())
                .subscribeWith(new MiSubScriber<ApiResult<TuftInfoEntity>>() {

                    @Override
                    protected void doOnStart() {
                        mView.showDialogLoading("");
                    }

                    @Override
                    protected void doOnNext(ApiResult<TuftInfoEntity> result) {
                        if (result.data != null) {
                            mView.returnTuftInfoData(result.data);
                            ToastUtils.showLong("保存成功");
                        }
                    }

                    @Override
                    protected void doOnComplete() {
                        mView.hideDialogLoading();
                    }

                    @Override
                    protected void doOnError(ApiException e) {
                        super.doOnError(e);
                        mView.hideDialogLoading();
                    }
                }));
    }
}
